package anonymouschat.security;

import java.io.Serializable;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.spec.IvParameterSpec;

import anonymouschat.utils.Print;

/**
 * Holds random numbers used as salt in local file encryptions
 * @author jnfrye
 * 
 */
public class KeySalt implements Serializable {

	private static final long serialVersionUID = 6651051088673383629L;

	private byte[] salt1;
	private byte[] salt2;
	private byte[] iv;

	/**
	 * Create a 2 new peices of salt, and an IV to be used for encryptions
	 */
	public KeySalt() {
		SecureRandom secRand = null;
		try {
			secRand = SecureRandom.getInstance("SHA1PRNG");
			salt1 = new byte[8];
			secRand.nextBytes(salt1);
			salt2 = new byte[8];
			secRand.nextBytes(salt2);
			iv = new byte[16];
			secRand.nextBytes(iv);
		} catch (NoSuchAlgorithmException e) {
			Print.error("Error getting random data", e);
		}
	}

	public byte[] getSalt1Bytes() {
		return salt1.clone();
	}
	
	public byte[] getSalt2Bytes() {
		return salt2.clone();
	}

	/**
	 * @return the iv
	 */
	public byte[] getIv() {
		return iv.clone();
	}

	/**
	 * Return the Initialization vector as an IvParamSpec object
	 * 
	 * @return
	 */
	public IvParameterSpec getIvParameterSpec() {
		return new IvParameterSpec(iv);
	}
}
